<%
  earliest = Time.at(@min_time)
  latest = Time.at(@max_time)

  # adjust to fall on day boundaries
  earliest = Time.mktime(earliest.year, earliest.month, earliest.day)
  latest = Time.mktime(latest.year, latest.month, latest.day)

  today = Time.mktime(Time.now.year, Time.now.month, Time.now.day)
  first_of_month = Time.mktime(@year || Time.now.year, @month || Time.now.month, 1) rescue today
  last_of_month = first_of_month.end_of_month

  first_day = first_of_month - first_of_month.wday.days
  last_day = last_of_month + (6 - last_of_month.wday).days

  d = first_day
-%>
<table class="<%= @css_prefix %>base" cellspacing="0" cellpadding="0" border="0">
  <tr class="<%= @css_prefix %>base <%= @css_prefix %>dow">
    <td class="<%= @css_prefix %>base <%= @css_prefix %>dow"><div class="<%= @css_prefix %>base <%= @css_prefix %>dow">S</div></td>
    <td class="<%= @css_prefix %>base <%= @css_prefix %>dow"><div class="<%= @css_prefix %>base <%= @css_prefix %>dow">M</div></td>
    <td class="<%= @css_prefix %>base <%= @css_prefix %>dow"><div class="<%= @css_prefix %>base <%= @css_prefix %>dow">T</div></td>
    <td class="<%= @css_prefix %>base <%= @css_prefix %>dow"><div class="<%= @css_prefix %>base <%= @css_prefix %>dow">W</div></td>
    <td class="<%= @css_prefix %>base <%= @css_prefix %>dow"><div class="<%= @css_prefix %>base <%= @css_prefix %>dow">T</div></td>
    <td class="<%= @css_prefix %>base <%= @css_prefix %>dow"><div class="<%= @css_prefix %>base <%= @css_prefix %>dow">F</div></td>
    <td class="<%= @css_prefix %>base <%= @css_prefix %>dow"><div class="<%= @css_prefix %>base <%= @css_prefix %>dow">S</div></td>
  </tr>
  <% while d <= last_day -%>
    <% if d.wday == 0 %>
  <tr class="<%= @css_prefix %>base">
    <% end -%>
    <td class="<%= @css_prefix %>base">
    <% if d >= first_of_month && d <= last_of_month -%>
      <div id="<%= @css_prefix %>day_<%= d.mday %>" class="<%= @css_prefix %>base <%= @css_prefix %>day_this_month<%= @event_days[d.mday] ? " #{@css_prefix}event_day " + (@event_days[d.mday].css_class || 'calendar1') : '' %><%= d == today ? " #{@css_prefix}today" : '' %>" <%= @event_days[d.mday] ? "onmouseover=\"this.className += ' #{@event_days[d.mday].css_class_hover || 'calendar1_hover'}'; showRollover(this.id, 0, 0, #{@popout_direction == 'right' ? 15 : -248}, 4);\" onmouseout=\"var classes = this.className.split(' '); classes.pop(); this.className = classes.join(' '); hideRollover(this.id);\"" : '' %>><div id="<%= @css_prefix %>day_<%= d.mday %>_std"><%= d.mday %></div>
      <% if evt = @event_days[d.mday] %>
      <div id="<%= @css_prefix %>day_<%= d.mday %>_hover" style="display: none;" class="<%= @css_prefix %>event_rollover">
        <table cellpadding="0" cellspacing="0" border="0" width="100%">
          <tr>
            <td background="/images/interface/bubble_top<%= @popout_direction == 'right' ? '' : '_left' %>.gif" style="padding: <%= @popout_direction == 'right' ? '4px 7px 0 20px' : '4px 20px 0 7px' %>">
              <%= d.strftime("%A, %B ") + d.strftime("%d").to_i.to_s + d.strftime(", %Y") %>
            </td>
          </tr>
          <tr>
            <td>
              <div class="<%= @css_prefix %>event_rollover_body" style="border-width: 0 1px 0 1px; border-style: solid; padding: 7px 7px 0 7px; margin-<%= @popout_direction == 'right' ? 'left' : 'right' %>: 12px;">
                <b><%= evt[:name] %></b>
                <%= '<br/>' + evt[:html] unless evt[:html].empty? %>
              </div>
            </td>
          </tr>
          <tr>
            <td>
              <img src="/images/interface/bubble_bottom<%= @popout_direction == 'right' ? '' : '_left' %>.gif" border="0" alt="" style="float: left" />
            </td>
          </tr>
        </table>
      </div>
      <% end -%>
    <% else -%>
      <div class="<%= @css_prefix %>base <%= @css_prefix %>day_other_month"><%= d.mday %></div>
    <% end -%>
    </div>
    </td>
    <% if d.wday == 6 -%>
  </tr>
    <% end -%>
    <% d += 1.day -%>
  <% end -%>
</table>